package gui.mainWindow;

import gui.DateFormListener;

import java.awt.Color;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import questions.Questions;



@SuppressWarnings("serial")
public class MainWindow extends JFrame{
	
	//window data
	private static final String title = "Domandino";
	//menu Area
	private static final JMenuItem prova = new JMenuItem("prova");
	private static final JMenu menu = new JMenu("Menu");
	private static final JMenuItem about = new JMenuItem("About");
	private static final JMenu info = new JMenu("Info");
	private static final JMenuBar menuBar = new JMenuBar();
	//date Area
	private static final JLabel dateLabel = new JLabel("Data Esame:   ");
	private static final JLabel dateForm = new JLabel(" not set");
 
	private MenuPanel menuPanel;
	private QuestionsPanel questionPanel;
	private Questions questions;

	private static SimpleDateFormat dateFormatter = new SimpleDateFormat("dd.MM.yy", Locale.ITALIAN);
	
	public MainWindow(Questions q){
		super(title);
		this.
		questions = q;
		menuPanel = new MenuPanel(questions, this);
		questionPanel = new QuestionsPanel(questions, this);
		
		//this.setMinimumSize(minimumSize);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setLocationRelativeTo(null);
		this.setResizable(true);
		
		
		//menu bar
			//menu
			menu.add(prova);
			//menuBar.add(menu);
			//about
			info.add(about);
			//menuBar.add(info);
			
			menuBar.add(Box.createHorizontalGlue());
			dateLabel.setForeground(Color.GRAY);
			menuBar.add(dateLabel);
			DateFormListener dateFormListener = new DateFormListener(dateForm, this);
			dateForm.addMouseListener(dateFormListener);
			menuBar.add(dateForm);
			menuBar.add(new JLabel("  "));
			
			
		this.setJMenuBar(menuBar);
	
		this.pack();
	}
	
	public void setDate(Date date){
		dateForm.setText(dateFormatter.format(date));
	}
	public void showMenuPanel(){
		try {
			menuPanel.updateButton();
			SwingUtilities.invokeAndWait(new SetPanel(this, menuPanel));
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
	}
	public void showQuestionsPanel(){
		try {
			SwingUtilities.invokeAndWait(new SetPanel(this, questionPanel));
			questionPanel.showQuestion(questions.getEntry());
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
	}
	public void reconfFrame(){
		this.pack();
		this.setMinimumSize(getPreferredSize());
		this.repaint();
	}
}


class SetPanel implements Runnable{
	
	private MainWindow mainW;
	private JPanel panel;
	
	public SetPanel(MainWindow main, JPanel p){
		mainW = main;
		panel = p;
	}
	public void run() {
		mainW.getContentPane().removeAll();
		mainW.getContentPane().add(panel);
		mainW.reconfFrame();
	}
}